//$URL: http://dasding.googlecode.com/svn/trunk/data/dao/mongoDB/src/main/java/de/piratech/dasding/data/database/mongo/EventCategoryDataImpl.java $
//$Id: EventCategoryDataImpl.java 118 2013-04-01 01:11:50Z krieg.maria@gmail.com $
/*
 * Copyright 2012 Piratech.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.piratech.dasding.data.database.mongo;

import java.util.Arrays;
import java.util.List;

import com.mongodb.DB;

import de.piratech.dasding.data.Event;
import de.piratech.dasding.data.EventCategory;
import de.piratech.dasding.data.User;
import de.piratech.dasding.data.database.DatabaseException;
import de.piratech.dasding.data.database.EventCategoryData;
import de.piratech.dasding.data.database.mongo.data.MongoEventCategory;
import de.piratech.dasding.data.database.mongo.data.MongoUser;

/**
 */
public class EventCategoryDataImpl extends
    AbstractMongoDB<MongoEventCategory, EventCategory> implements
    EventCategoryData {

  public EventCategoryDataImpl(DB pDB) {
    super(pDB, MongoEventCategory.class, COLLECTIONS.EVENTCATEGORY_COLLECTION);
  }

  @Override
  public EventCategory createEventCategory(EventCategory pEventCategory)
      throws DatabaseException {
    if (pEventCategory.getCreatedBy() == null) {
      throw new DatabaseException("creator is null");
    }
    checkObjectExists(COLLECTIONS.USER_COLLECTION.getName(),
        pEventCategory.getCreatedBy());
    return getDBCollection().insert(new MongoEventCategory(pEventCategory))
        .getSavedObject().getDataObject();
  }

  @Override
  public EventCategory getEventCategory(String pId) {
    return getDBCollection().findOneById(pId).getDataObject();
  }

  @Override
  public void updateEventCategory(EventCategory pEventCategory) {
    getDBCollection().updateById(pEventCategory.getId(),
        new MongoEventCategory(pEventCategory));
  }

  @Override
  public void deleteEventCategory(EventCategory pEventCategory)
      throws DatabaseException {
    List<Event> relatedEvents = new EventDataImpl(db).findByCategories(Arrays
        .asList(new String[] { pEventCategory.getId() }));
    if (relatedEvents == null || relatedEvents.size() > 0) {
      throw new DatabaseException("there are related events");
    }
    UserDataImpl userDataImpl = new UserDataImpl(db);
    List<User> users = userDataImpl.findUsersByEventCategory(pEventCategory);
    for (User user : users) {
      user.removeInterestedIn(pEventCategory);
      userDataImpl.updateUser(user);
    }
    getDBCollection().removeById(pEventCategory.getId());
  }

  @Override
  public List<EventCategory> getCategories() {
    List<MongoEventCategory> array = getDBCollection().find().toArray();
    List<EventCategory> events = getDataObjectList(array);
    return events;
  }

  @Override
  public List<EventCategory> getVerifiedCategories() {
    return findByVerified(true);
  }

  @Override
  public List<EventCategory> getNotVerifiedCategories() {
    return findByVerified(false);
  }

  public List<EventCategory> findByVerified(boolean pVerified) {
    List<MongoEventCategory> array = getDBCollection().find()
        .is("verified", pVerified).toArray();
    List<EventCategory> events = getDataObjectList(array);
    return events;
  }

  public List<EventCategory> findByCreated(User pUser) {
    List<MongoEventCategory> findByObjectReference = findByObjectReference(
        "createdByRef", COLLECTIONS.EVENTCATEGORY_COLLECTION.getName(),
        pUser.getId(), MongoUser.class);
    List<EventCategory> list = getDataObjectList(findByObjectReference);
    return list;
  }
}
